# /bin/bash

if [ "$(id -u)" -ne 0 ]; then
    echo "请使用sudo执行此脚本"
    exit 1
fi

apt install golang -y

go --version

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

echo "进行安装瑜伽馆的超级管理员工具"

if test -f ./.yogaconfig.json ; then
    echo "目录正确"
else
    echo "缺少.yogaconfig.json文件,请检查是否位于正确的目录"
    exit 1
fi

cp ./.yogaconfig.json ~/

if [ $? -ne 0 ];then
    echo "复制.yogaconfig.json文件失败"
    exit 1
fi

if test -d ../../cliYoga;then
    echo "cli目录正确"
else
    echo "缺少源文件"
    exit 1
fi

cd  ../../cliYoga

if test -d ./output;then
    rm ./output/*
else
    mkdir ./output
fi

if test -d ./src ;then
    cd ./src
else
    echo "缺少源文件目录"
    exit 1
fi



echo "开始进行构建"


go build -o ../output/cli

if [ $? -ne 0 ];then
    echo "构建失败"
    echo "很有可能因为时间太久而导致安装失败"
    echo "特别是构建这一步如果构建失败最好自行修改脚本,尝试切换代理解决问题"
    echo "如果实在不行请联系作者 yang15279925030"
    exit 1
fi

cd ../

rm /usr/local/bin/yoga

cp ./output/cli /usr/local/bin/yoga
if [ $? -ne 0 ];then
    echo "复制失败"
    exit 1
fi
echo "安装成功"
echo "使用yoga 检查是否安装成功"
echo "使用yoga --help查看使用方式"
